package com.wangzh.concurrent.lock.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

//异步调用

/**
 * 异步执行
 * 成功回调
 * 失败回调
 */
public class Demo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() ->{
//            try {
//                TimeUnit.SECONDS.sleep(2);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println("niHao:"+Thread.currentThread().getName());
//        });
//        System.out.println("1111");
//        Void unused = completableFuture.get(); //获取阻塞结果


        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() ->{
            int i = 1/0;
            return 1024;
        });
//        Integer i = completableFuture1.get();
//        System.out.println(i);
        completableFuture1.whenComplete((t,u) ->{
                    System.out.println("t:"+t);
                    System.out.println("u:"+u);
                })
                .exceptionally(e ->{
                    System.out.println(e.getMessage());
                    return 500;
                }).get();
    }
}
